﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Linq;
using WPFExcelReport.Helpers;
using System.Windows.Input;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// 专用于坯布检验
    /// 1.扫描布卷号，进入验布，读取生产单信息
    /// 2.输入长度，修改数据库记录
    /// 3.剪出新布卷
    /// 4.打印标签
    /// 5.疵点保存修正
    /// 6.事件记录
    /// 7.纬纱扣除
    /// 8.织轴扣除
    /// </summary>
    public partial class TWB19YP
    {
        
        public int pos = 700;
        public TWB19YP()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBFEHL();
            this.DataContext = MV;
            InitializeComponent();
        }
        DataRow dwFWDC = null;
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                WBFQPKT = "";
            }
            else
            {
                WBFQPKT = (TMV.WBFQPKT).ToString();
            }
        }
        public override void OnFWB08KEYChg(string value)
        {
            cvMaster.CustomFilter = null;
            if (TMV == null)
                return;
            if (value.Length < 12)
                return;
            dwFWDC = TBMain.Clone().NewRow();
            TMV.SetUpNewRow(ref dwFWDC);
            dwFWDC["WBFMANDANT"] = app.Mandant;
            dwFWDC["WBFRWKRS"] = value.Substring(0, 2);
            dwFWDC["WBFRWLFD"] = value.Substring(2, 6);
            dwFWDC["WBFRWPOS"] = value.Substring(8, 4);
            dwFWDC["WBFVERSION"] = int.Parse(value.Substring(12));
            dwFWDC["WBFFSCHL"] = " AND WBFFSCHL='YPN'";
            filter = "WBFBRTVON=" + double.Parse(value.Substring(8, 4));
            cvMaster.CustomFilter = filter;
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length == 0)
                return;
            KRS = EXAUF.Substring(0, 2);
            LFD = EXAUF.Substring(2, 6);
            POS = EXAUF.Substring(8, 4);
            ERVER = int.Parse(EXAUF.Substring(12));
            base.OnEXAUFChg(vlaue);
        }
        public override void OnDESChg(string des)
        {
            TBMain.Clear();
            if (des.Length == 0)
                return;
            pos = 700;
            KRS = des.Substring(0, 2);
            LFD = des.Substring(2, 6);
            POS = des.Substring(8, 4);
            ERVER = int.Parse(des.Substring(12));
            dwData = TBMain.NewRow();
            dwData["WBFMANDANT"] = app.Mandant;
            dwData["WBFRWKRS"] = des.Substring(0, 2);
            dwData["WBFRWLFD"] = des.Substring(2, 6);
            dwData["WBFRWPOS"] = des.Substring(8, 4);
            dwData["WBFVERSION"] = int.Parse(des.Substring(12));
            dwData["WBFFSCHL"] = " AND WBFFSCHL='YPN'";
            SResult = TMV.QWBFEHL04(dwData);
            MergeData(SResult, false);
            if (TBMain.Rows.Count > 0)
            {
                var rm = (from DataRow row in TBMain.AsEnumerable()
                          select Convert.ToInt16(row["WBFFPOS"].ToString()))
                 .Max();
                pos = int.Parse(rm.ToString());
            }
            base.OnDESChg(des);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                vwMaster.View.MoveCurrentToFirst();
            }
            if (dwMaster == null)
                return;
            if (TMV.WBFLGVON < 0)
            {
                TMV.WBFLGVON = CMTR;
                TMV.WBFLGBIS = CMTR;
            }
            TMV.UpdateWBFEHL(dwMaster, 0);
            PM["RWKRS"] = dwMaster["WBFRWKRS"].ToString();
            PM["RWLFD"] = dwMaster["WBFRWLFD"].ToString();
            PM["RWPOS"] = dwMaster["WBFRWPOS"].ToString();
            PM["VERSION"] = dwMaster["WBFVERSION"].ToString();
            PM["ROLLLG"] = "0";
            TMV.SETSHIFT(PM);
            MapToFWB(dwMaster);
            PM["RWKRS"] = dwFWDC["WBFRWKRS"].ToString();
            PM["RWLFD"] = dwFWDC["WBFRWLFD"].ToString();
            PM["RWPOS"] = dwFWDC["WBFRWPOS"].ToString();
            PM["VERSION"] = dwFWDC["WBFVERSION"].ToString();
            PM["ROLLLG"] = "0";
            TMV.SETSHIFT(PM);
            //base.SaveCommand(sender, e);
        }
        public void MapToFWB(DataRow dwPB, bool IsInsert = true, bool IsDelete = true)
        {
            if (dwFWDC == null)
                return;
            #region 
            dwFWDC["WBFFPOS"] = dwPB["WBFFPOS"];
            dwFWDC["WBFFSCHL"] = dwPB["WBFFSCHL"];
            dwFWDC["WBFLGVON"] = dwPB["WBFLGVON"];
            dwFWDC["WBFLGBIS"] = dwPB["WBFLGBIS"];
            dwFWDC["WBFBRTVON"] = dwPB["WBFBRTVON"];
            dwFWDC["WBFBRTBIS"] = dwPB["WBFBRTBIS"];
            dwFWDC["WBFVGUETKZ"] = dwPB["WBFVGUETKZ"];
            dwFWDC["WBFMARKART"] = dwPB["WBFMARKART"];
            dwFWDC["WBFSEITE"] = dwPB["WBFSEITE"];
            dwFWDC["WBFRICHT"] = dwPB["WBFRICHT"];
            dwFWDC["WBFUMRECH"] = dwPB["WBFUMRECH"];
            dwFWDC["WBFZENSUR"] = dwPB["WBFZENSUR"];
            dwFWDC["WBFZENSURKZ"] = dwPB["WBFZENSURKZ"];
            dwFWDC["WBFRWPOST"] = dwPB["WBFRWPOST"];
            dwFWDC["WBFQPKT"] = dwPB["WBFQPKT"];
            dwFWDC["WBFSTATKZ"] = dwPB["WBFSTATKZ"];
            dwFWDC["WBFBAHN"] = dwPB["WBFBAHN"];
            dwFWDC["WBFGWT"] = dwPB["WBFGWT"];
            dwFWDC["WBFVGUET"] = dwPB["WBFVGUET"];
            dwFWDC["WBFFEART"] = dwPB["WBFFEART"];
            dwFWDC["WBFNETTOKZ"] = dwPB["WBFNETTOKZ"];
            dwFWDC["WBFTYP"] = dwPB["WBFTYP"];
            dwFWDC["WBFFILLER"] = dwPB["WBFFILLER"];
            if (IsDelete)
            {
                TMV.DWFE01(dwFWDC);
            }
            if (IsInsert)
            {
                TMV.InsertWBFEHL(dwFWDC);
            }
            #endregion
        } 
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                pos++;
                dwData = TBMain.NewRow();
                TMV.SetUpNewRow(ref dwData);
                dwData["WBFMANDANT"] = app.Mandant;
                dwData["WBFRWKRS"] = KRS;
                dwData["WBFRWLFD"] = LFD;
                dwData["WBFRWPOS"] = POS;
                dwData["WBFVERSION"] = ERVER;
                dwData["WBFFPOS"] = pos;
                dwData["WBFLGVON"] = 0;
                dwData["WBFLGBIS"] = 0;
                dwData["WBFFSCHL"] = "YPN";
                dwData["WBFQPKT"] = 0;
                dwData["WBFFILLER"] = 0;
                if (dwFWDC != null)
                {
                    dwData["WBFBRTVON"] = dwFWDC["WBFRWPOS"];
                }
                TBMain.Rows.InsertAt(dwData, 0);
                TMV.InsertWBFEHL(dwData);
                MapToFWB(dwData, true);
                vwMaster.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            //base.NewCommand(sender, e);
        }
        public override void OnShifProdChg()
        {
            try
            {

                if (SHIFTPRODUCTION == "")
                    return;
                if (SHIFTPRODUCTION.Length < 30)
                {
                    string rs = "剪样类型10:剪样10:数量";
                    MessageBox.Show("信息不完善:" + SHIFTPRODUCTION + ";" + rs);
                    return;
                } 
                pos++;
                dwData = TBMain.NewRow();
                TMV.SetUpNewRow(ref dwData);
                dwData["WBFMANDANT"] = app.Mandant;
                dwData["WBFRWKRS"] = KRS;
                dwData["WBFRWLFD"] = LFD;
                dwData["WBFRWPOS"] = POS;
                dwData["WBFVERSION"] = ERVER;
                dwData["WBFFPOS"] = pos;
                dwData["WBFLGVON"] =1000* double.Parse(SHIFTPRODUCTION.Substring(20, 10));
                dwData["WBFLGBIS"] = 1000 * double.Parse(SHIFTPRODUCTION.Substring(20, 10));
                dwData["WBFFSCHL"] = "YPN";
                dwData["WBFQPKT"] = double.Parse(SHIFTPRODUCTION.Substring(0, 10));
                dwData["WBFFILLER"] = Helper.Left(Helper.ConvertGB2312To8859P1(SHIFTPRODUCTION.Substring(10, 10)).PadRight(12), 12);
                if (dwFWDC != null)
                {
                    dwData["WBFBRTVON"] = dwFWDC["WBFRWPOS"];
                }

                TBMain.Rows.Add(dwData);
                TMV.InsertWBFEHL(dwData);
                MapToFWB(dwData, true);
                base.OnShifProdChg();
            }
            catch (Exception ex)
            {

            }

        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            string key = KEY;
            filter = "WBFRWKRS+WBFRWLFD+WBFRWPOS+WBFVERSION+WBFFPOS='" + KEY + "'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                TMV.DWFE01(row);
                MapToFWB(row);
                TMV.DWFE01(dwFWDC);
                row.Delete();
            }
            TBMain.AcceptChanges();
        } 
    }
}
